package com.xja.dubbo.controller;

import com.xja.dubbo.entity.Easybuy_product;
import com.xja.dubbo.entity.Easybuy_product_category;
import com.xja.dubbo.entity.Easybuy_user;
import com.xja.dubbo.service.ProductCategoryService;
import com.xja.dubbo.service.ProductService;
import com.xja.dubbo.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ycz
 * @create 2021-12-17-15:58
 */
@Controller
public class IndexController {

    private Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Reference(url = "dubbo://localhost:20881")
    private ProductService productService;

    @Reference(url = "dubbo://localhost:20881")
    private ProductCategoryService productCategoryService;

    @Reference(url = "dubbo://localhost:20882")
    private UserService userService;


    @RequestMapping("index")
    public String index(Model model,@CookieValue(required = false) String uuid){
        try {
            logger.info("展示主页面");

            if(uuid!=null && !"".equals(uuid)){
                Easybuy_user loginUser = userService.findLoginUserFromRedis(uuid);
                model.addAttribute("loginUser",loginUser);
            }
            //获取一级分类
            List<Easybuy_product_category> firstCategory = productCategoryService.findCategoryByPid(0);

            //每个一级分类下的商品列表
            Map<Integer,List<Easybuy_product>> productMap = new HashMap<>();

            //二级分类
            Map<Integer,List<Easybuy_product_category>> secondMap = new HashMap<>();

            //三级分类
            Map<Integer,List<Easybuy_product_category>> thirdMap = new HashMap<>();

            //获取二级分类
            for (Easybuy_product_category category : firstCategory) {

                //一级分类下的首页商品列表6个
                List<Easybuy_product> productList = productService.findIndexProByCategory(category.getId());
                productMap.put(category.getId(), productList);

                //二级分类
                List<Easybuy_product_category> secondCategoryList = productCategoryService.findCategoryByPid(category.getId());

                secondMap.put(category.getId(), secondCategoryList);

                //获取三级分类
                for (Easybuy_product_category secondCategory : secondCategoryList) {

                    List<Easybuy_product_category> thirdCategoryList = productCategoryService.findCategoryByPid(secondCategory.getId());

                    thirdMap.put(secondCategory.getId(), thirdCategoryList);

                }

            }

            model.addAttribute("firstCategory",firstCategory);
            model.addAttribute("productMap",productMap);
            model.addAttribute("thirdMap",thirdMap);
            model.addAttribute("secondMap",secondMap);

            return "Index";
        } catch (Exception e) {
            logger.error("展示失败", e);
        }
        return "redirect:error.html";
    }

}
